import { reactive } from 'vue';

const state: {
  list: {
    id?: string;
    title?: string;
    /**
     * 1 审批
     * 2 抄送
     */
    flowType?: number;
    /**
     * 审批 1 依次审批 2 会签 3 或签
     * 抄送 2 会签
     */
    type?: number;
    /**
     * 审批/抄送 人员
     */
    persons: FlowPathTemplateApi.ApproverPerson[];
    /**
     * 是否支持修改人员（包括：添加/删除）
     */
    isModify: boolean;
  }[];
} = reactive({
  list: [],
});

/**
 * 遍历流程图每个节点
 */
function traverseFlowGraph(currentList: FlowPathTemplateApi.FlowGraph | undefined) {
  if (!currentList) {
    return;
  }
  const flowType = currentList.flowType;
  if (flowType === 1) {
    if (!approveType('审批人', currentList)) {
      return;
    }
  } else if (flowType === 2) {
    if (!copyType('抄送人', currentList)) {
      return;
    }
  }
  if (!currentList.nextList) {
    return;
  }
  traverseFlowGraph(currentList.nextList[0]);
}

/**
 * 审批节点
 * @param title 节点标题
 * @param currentList 当前节点
 * @returns boolean true 执行成功 false 执行失败
 */
function approveType(title: string, currentList: FlowPathTemplateApi.FlowGraph): boolean {
  if (!currentList.approverDto) {
    return false;
  }

  const approveSource = currentList.approverDto.approverSource;
  let persons: FlowPathTemplateApi.ApproverPerson[] = [];
  let isModify = true;
  //审批人来源（1-指定审批人，2-审批人自己，3-部门主管，4-发起人自选，5-角色）
  if (approveSource !== 4) {
    if (!currentList.approverDto.approverVsetList) {
      return false;
    }
    persons = currentList.approverDto.approverVsetList;
    // 3部门主管、5角色，当审批人为空时，4发起人自选
    if (
      (approveSource === 3 || approveSource === 5) &&
      currentList.approverDto.noApproveType === 4
    ) {
      isModify = true;
    } else {
      isModify = false;
    }
  } else {
    if (
      currentList.approverDto.approverVsetList &&
      currentList.approverDto.approverVsetList.length > 0
    ) {
      persons = currentList.approverDto.approverVsetList;
    }
  }
  const multiApproveType = currentList.approverDto.multiApproveType;
  state.list.push({
    id: currentList.id,
    title,
    flowType: 1,
    type: multiApproveType,
    persons,
    isModify,
  });
  return true;
}

/**
 * 抄送节点
 * @param title 节点标题
 * @param currentList 当前节点
 */
function copyType(title: string, currentList: FlowPathTemplateApi.FlowGraph) {
  if (!currentList.copytoDto) {
    return false;
  }

  let persons: FlowPathTemplateApi.ApproverPerson[] = [];
  let isModify = true;
  const copytoType = currentList.copytoDto.copytoType;
  //1 发起人自选 2 指定抄送人
  if (copytoType === 2) {
    if (!currentList.copytoDto.copytoVsetList) {
      return false;
    }
    persons = currentList.copytoDto.copytoVsetList;
    isModify = false;
  } else {
    if (currentList.copytoDto.copytoVsetList && currentList.copytoDto.copytoVsetList.length > 0) {
      persons = currentList.copytoDto.copytoVsetList;
    }
  }
  state.list.push({
    id: currentList.id,
    title,
    flowType: 2,
    type: 2,
    persons,
    isModify,
  });
  return true;
}

/**
 * 遍历流程图每个节点
 */
function traverseFlowGraphAddPersons(
  currentList: FlowPathTemplateApi.FlowGraph | undefined,
  index: number,
) {
  if (!currentList) {
    return;
  }
  const flowType = currentList.flowType;
  if (flowType === 1) {
    if (!approveTypeAddPersons(currentList, index)) {
      return;
    }
  } else if (flowType === 2) {
    if (!copyTypeAddPersons(currentList, index)) {
      return;
    }
  }
  if (!currentList.nextList) {
    return;
  }
  traverseFlowGraphAddPersons(currentList.nextList[0], ++index);
}

function approveTypeAddPersons(currentList: FlowPathTemplateApi.FlowGraph, index: number) {
  if (!currentList.approverDto) {
    return false;
  }
  const approveSource = currentList.approverDto.approverSource;
  if (approveSource === 4 && state.list[index].persons.length > 0) {
    currentList.approverDto.approverVsetList = state.list[index].persons;
  }
  return true;
}

/**
 * 抄送节点
 * @param title 节点标题
 * @param currentList 当前节点
 */
function copyTypeAddPersons(currentList: FlowPathTemplateApi.FlowGraph, index: number) {
  if (!currentList.copytoDto) {
    return false;
  }

  const copytoType = currentList.copytoDto.copytoType;
  //1 发起人自选 2 指定抄送人
  if (copytoType === 1 && state.list[index].persons.length > 0) {
    currentList.copytoDto.copytoVsetList = state.list[index].persons;
  }
  return true;
}

export { state, traverseFlowGraph, traverseFlowGraphAddPersons };
